package com.study.fetch;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/4/5 16:12
 */
public class Demo {
    static List<ETF> ETFS = Lists.newArrayList(new ETF("512100", "中证1000", "100"),

            new ETF("159949", "创业板50", "949"), new ETF("159985", "豆粕ETF", "985"), new ETF("513100", "纳指ETF", "100"),
            new ETF("511880", "银华日历", "880"), new ETF("515300", "红利低波", "300"));

    @Test
    public void fetchRk() {
        for (ETF etf : ETFS) {
            String url = "https://hq.stock.sohu.com/mkline/cn/{}/cn_{}-10_2.html?_={}";

            System.out.println(etf);
            String body = HttpRequest.get(StrUtil.format(url, etf.getUrlCn(), etf.getBondId(), System.currentTimeMillis())).execute().body();
            String reqBody = StrUtil.subBetween(body, "quote_d_dividend(", ")");

            HistPriceZsDTO histPriceZsDTO = JSON.parseObject(reqBody, HistPriceZsDTO.class);
            List<HistDetailDTO> histDetailDTOS = histPriceZsDTO.getDataDiv().stream().map(HistDetailDTO::parse).collect(Collectors.toList());

            System.out.println(histDetailDTOS);
        }
    }

    @Data
    @Accessors(chain = true)
    public static class HistPriceZsDTO {
        private int status;

        private List<String> dividend;

        private String type;

        private String statusmsg;

        private List<List<String>> dataBasic;

        private List<List<String>> dataDiv;


    }

    @Data
    @Accessors(chain = true)
    @ExcelIgnoreUnannotated
    public static class HistDetailDTO {
        @ExcelProperty(value = "代码")
        private String bondId;
        @ExcelProperty(value = "名称")
        private String bondName;

        @ColumnWidth(15)
        @ExcelProperty(value = "数据日期")
        private LocalDate date;
        //@ExcelProperty(value = "开盘")
        private BigDecimal beginPrice;

        //@ExcelProperty(value = "收盘")
        private BigDecimal endPrice;
        //@ExcelProperty(value = "涨跌额")
        private BigDecimal incrPrice;
        @ColumnWidth(15)
        @ExcelProperty(value = "涨跌幅(%)")
        private BigDecimal incrAmount;

        private BigDecimal minPrice;

        private BigDecimal maxPrice;

        /**
         * 成交金额(万)
         */
        private BigDecimal volumes;
        //
        @ExcelProperty(value = "成交量")
        private BigDecimal volume;

        public static HistDetailDTO parse(List<String> lines) {
            if (CollectionUtils.isEmpty(lines)) {
                return null;
            }

            HistDetailDTO histDetailDTO = new HistDetailDTO();
            histDetailDTO.setDate(LocalDateTimeUtil.parseDate(lines.get(0), "yyyMMdd"));
            histDetailDTO.setBeginPrice(new BigDecimal(lines.get(1)));
            histDetailDTO.setEndPrice(new BigDecimal(lines.get(2)));

            histDetailDTO.setMaxPrice(new BigDecimal(lines.get(3)));
            histDetailDTO.setMinPrice(new BigDecimal(lines.get(4)));
            histDetailDTO.setVolume(new BigDecimal(lines.get(5)));
            histDetailDTO.setVolumes(new BigDecimal(lines.get(6)));

            histDetailDTO.setIncrPrice(new BigDecimal(lines.get(8)));
            histDetailDTO.setIncrAmount(new BigDecimal(StringUtil.replace(lines.get(9), "%", "")));
            return histDetailDTO;
        }

    }

    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ETF {
        private String bondId;
        @ExcelProperty(value = "名称")
        private String bondName;

        private String urlCn;
    }

}
